#include "crc16.h"

unsigned int crc_table[16]=
{ 
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
};

#define LOBYTE(w) ((unsigned char )(w)) 

unsigned short Crc16(void *lptr,  unsigned int len) 
	{
	unsigned short crc ;
	unsigned char da  ;
	unsigned int nlen ;
	unsigned char *ptr ;

	ptr = (unsigned char*)lptr;

	nlen = len;
	crc = 0 ;
	da = 0 ;

	while(nlen--!=0)
		{
		da = LOBYTE(crc>>12); 
		crc <<= 4; 
		crc ^= crc_table[da^(*ptr/16)]; 

		da = crc>>12; 
		crc <<= 4; 
		crc ^= crc_table[da^(*ptr&0x0f)]; 
		ptr++;
		}

	return(crc);
	} 